package com.example.vehiclemanagesystem.controller;

import com.example.vehiclemanagesystem.entity.Car;
import com.example.vehiclemanagesystem.entity.Driver;
import com.example.vehiclemanagesystem.entity.Notice;
import com.example.vehiclemanagesystem.entity.Route;
import com.example.vehiclemanagesystem.service.DriverService;
import com.example.vehiclemanagesystem.service.NoticeService;
import com.example.vehiclemanagesystem.service.RouteService;
import net.bytebuddy.implementation.bytecode.assign.TypeCasting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class RouteController {
    @Autowired
    private RouteService routeService;
    @Autowired
    private NoticeService noticeService;

    @PostMapping("/addRoute")
    public Map<String, Object> addRoute(@RequestBody Map<String, String> remap) {
       Integer d_id=Integer.parseInt(remap.get("d_id"))	;
       String way=remap.get("way")	;
       String routes=remap.get("route")	;
       String date1=remap.get("date1")	    ;
       date1=date1.substring(0,19);
       String date2=remap.get("date2");
       String type=remap.get("type");
        Map<String, Object> map = new HashMap<>();
        try {
            Route route=new Route(d_id,way,routes,type,date1,date2);
            routeService.addNewRoute(route);
            map.put("success",true);
            map.put("message","添加路线成功");
            String docx="路线修改为： "+"司机id:"+String.valueOf(d_id)+"; 路线："+routes+";生效时间:"+date1;
            Notice notice=new Notice(true,"路线新增通知","更改车辆路线",docx,"","","乘客用户");
            noticeService.addNewNotice(notice);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "添加失败！");
        }
        return map;
    }
    @PostMapping("/deleteRoute")
    public Map<String, Object> deleteRoute(@RequestBody Map<String, String> remap) {
        Integer r_id = Integer.parseInt(remap.get("r_id"));
        Map<String, Object> map = new HashMap<>();
        try {
            Route route =routeService.selectRouteByRouteID(r_id);
            if (route == null) {
                map.put("success", false);
                map.put("message", "路线不存在");
            } else {
                routeService.deleteRoute(route);
                map.put("success", true);
                map.put("message",/*d_id+*/"路线删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "操作失败！");
        }
        return map;
    }
    @PostMapping("/updateRoute")
    public Map<String, Object> updateRoute(@RequestBody Map<String, String> remap) {
        Integer r_id=Integer.parseInt(remap.get("r_id"))	;
        String start=remap.get("start")	;
        String terminal=remap.get("terminal")	;
        String site=remap.get("site")	    ;
        Integer quantity=Integer.parseInt(remap.get("quantity"));
        Map<String, Object> map = new HashMap<>();
        try {
            Route route1=routeService.selectRouteByRouteID(r_id);
            if (route1 == null) {
                map.put("success", false);
                map.put("message", "路线不存在");
            } else {
                //Route route=new Route(r_id,start,terminal,site,quantity);
               // routeService.updateRoute(route);
                map.put("success", true);
                map.put("message",/*d_id+*/"路线修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "操作失败！");
        }
        return map;
    }

    @RequestMapping("/searchRoute")
    public  List<Map<String,Object>> searchRoute(@RequestParam("query") String input) {
        List<Route> routes = routeService.selectRouteAll();
        Map<String,Object> map = new HashMap<>();
        List<Map<String,Object>> mapList = new ArrayList<>();
        String type = "routes";
        for (Route route : routes){
            Map<String,Object> map1 = new HashMap<>();
            map1.put("text",route.getR_id());
            map1.put("value",route.getR_id());
           // map1.put("type",type);
            mapList.add(map1);
        }
        //map.put("type","routes");
        mapList.add(map);
        System.out.println(map.toString());
        return mapList;
    }
}
